Skip to main content

alloc/collections/vec_deque/
splice.rs

1use core::alloc::Allocator;
2
3use crate::alloc::Global;
4use crate::collections::vec_deque::Drain;
5use crate::vec::Vec;
6
7/// A splicing iterator for `VecDeque`.
8///
9/// This struct is created by [`VecDeque::splice()`][super::VecDeque::splice].
10/// See its documentation for more.
11///
12/// # Example
13///
14/// ```
15/// # #![feature(deque_extend_front)]
16/// # use std::collections::VecDeque;
17///
18/// let mut v = VecDeque::from(vec![0, 1, 2]);
19/// let new = [7, 8];
20/// let iter: std::collections::vec_deque::Splice<'_, _> = v.splice(1.., new);
21/// ```
22#[unstable(feature = "deque_extend_front", issue = "146975")]
23#[derive(Debug)]
24pub struct Splice<
25    'a,
26    I: Iterator + 'a,
27    #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global,
28> {
29    pub(super) drain: Drain<'a, I::Item, A>,
30    pub(super) replace_with: I,
31}
32
33#[unstable(feature = "deque_extend_front", issue = "146975")]
34impl<I: Iterator, A: Allocator> Iterator for Splice<'_, I, A> {
35    type Item = I::Item;
36
37    fn next(&mut self) -> Option<Self::Item> {
38        self.drain.next()
39    }
40
41    fn size_hint(&self) -> (usize, Option<usize>) {
42        self.drain.size_hint()
43    }
44}
45
46#[unstable(feature = "deque_extend_front", issue = "146975")]
47impl<I: Iterator, A: Allocator> DoubleEndedIterator for Splice<'_, I, A> {
48    fn next_back(&mut self) -> Option<Self::Item> {
49        self.drain.next_back()
50    }
51}
52
53#[unstable(feature = "deque_extend_front", issue = "146975")]
54impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> {}
55
56// See also: [`crate::vec::Splice`].
57#[unstable(feature = "deque_extend_front", issue = "146975")]
58impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> {
59    fn drop(&mut self) {
60        // This will set drain.remaining to 0, so its drop won't try to read deallocated memory on
61        // drop.
62        self.drain.by_ref().for_each(drop);
63
64        // At this point draining is done and the only remaining tasks are splicing
65        // and moving things into the final place.
66
67        unsafe {
68            let tail_len = self.drain.tail_len; // #elements behind the drain
69
70            if tail_len == 0 {
71                self.drain.deque.as_mut().extend(self.replace_with.by_ref());
72                return;
73            }
74
75            // First fill the range left by drain().
76            if !self.drain.fill(&mut self.replace_with) {
77                return;
78            }
79
80            // There may be more elements. Use the lower bound as an estimate.
81            // FIXME: Is the upper bound a better guess? Or something else?
82            let (lower_bound, _upper_bound) = self.replace_with.size_hint();
83            if lower_bound > 0 {
84                self.drain.move_tail(lower_bound);
85                if !self.drain.fill(&mut self.replace_with) {
86                    return;
87                }
88            }
89
90            // Collect any remaining elements.
91            // This is a zero-length vector which does not allocate if `lower_bound` was exact.
92            let mut collected = self.replace_with.by_ref().collect::<Vec<I::Item>>().into_iter();
93            // Now we have an exact count.
94            if collected.len() > 0 {
95                self.drain.move_tail(collected.len());
96                let filled = self.drain.fill(&mut collected);
97                debug_assert!(filled);
98                debug_assert_eq!(collected.len(), 0);
99            }
100        }
101        // Let `Drain::drop` move the tail back if necessary and restore `deque.len`.
102    }
103}
104
105/// Private helper methods for `Splice::drop`
106impl<T, A: Allocator> Drain<'_, T, A> {
107    /// The range from `self.deque.len` to `self.deque.len + self.drain_len` contains elements that
108    /// have been moved out.
109    /// Fill that range as much as possible with new elements from the `replace_with` iterator.
110    /// Returns `true` if we filled the entire range. (`replace_with.next()` didn’t return `None`.)
111    ///
112    /// # Safety
113    ///
114    /// self.deque must be valid. self.deque.len and self.deque.len + self.drain_len must be less
115    /// than twice the deque's capacity.
116    unsafe fn fill<I: Iterator<Item = T>>(&mut self, replace_with: &mut I) -> bool {
117        let deque = unsafe { self.deque.as_mut() };
118        let range_start = deque.len;
119        let range_end = range_start + self.drain_len;
120
121        for idx in range_start..range_end {
122            if let Some(new_item) = replace_with.next() {
123                let index = deque.to_physical_idx(idx);
124                unsafe { deque.buffer_write(index, new_item) };
125                deque.len += 1;
126                self.drain_len -= 1;
127            } else {
128                return false;
129            }
130        }
131        true
132    }
133
134    /// Makes room for inserting more elements before the tail.
135    ///
136    /// # Safety
137    ///
138    /// self.deque must be valid.
139    unsafe fn move_tail(&mut self, additional: usize) {
140        let deque = unsafe { self.deque.as_mut() };
141
142        // `Drain::new` modifies the deque's len (so does `Drain::fill` here)
143        // directly with the start bound of the range passed into
144        // `VecDeque::splice`. This causes a few different issue:
145        //     - Most notably, there will be a hole at the end of the
146        //       buffer when our buffer resizes in the case that our
147        //       data wraps around.
148        //     - We cannot use `VecDeque::reserve` directly because
149        //       how it reserves more space and updates the `VecDeque`'s
150        //       `head` field accordingly depends on the `VecDeque`'s
151        //       actual `len`.
152        //     - We cannot just directly modify `VecDeque`'s `len` and
153        //       and call `VecDeque::reserve` afterward because if
154        //       `VecDeque::reserve` panics on capacity overflow,
155        //       well now our `VecDeque`'s head does not get updated
156        //       and we still have a potential hole at the end of the
157        //       buffer.
158        // Therefore, we manually reserve additional space (if necessary)
159        // based on calculating the actual `len` of the `VecDeque` and adjust
160        // `VecDeque`'s len right *after* the panicking region of `VecDeque::reserve`
161        // (that is `RawVec` `reserve()` call)
162
163        let drain_start = deque.len;
164        let tail_start = drain_start + self.drain_len;
165
166        // Actual VecDeque's len = drain_start + tail_len + drain_len
167        let actual_len = drain_start + self.tail_len + self.drain_len;
168        let new_cap = actual_len.checked_add(additional).expect("capacity overflow");
169        let old_cap = deque.capacity();
170
171        if new_cap > old_cap {
172            deque.buf.reserve(actual_len, additional);
173            // If new_cap doesn't panic, we can safely set the `VecDeque` len to its
174            // actual len; this needs to be done in order to set deque.head correctly
175            // on `VecDeque::handle_capacity_increase`
176            deque.len = actual_len;
177            // SAFETY: this cannot panic since our internal buffer's new_cap should
178            // be bigger than the passed in old_cap
179            unsafe {
180                deque.handle_capacity_increase(old_cap);
181            }
182        }
183
184        let new_tail_start = tail_start + additional;
185        unsafe {
186            deque.wrap_copy(
187                deque.to_physical_idx(tail_start),
188                deque.to_physical_idx(new_tail_start),
189                self.tail_len,
190            );
191        }
192
193        // revert the `VecDeque` len to what it was before
194        deque.len = drain_start;
195        self.drain_len += additional;
196    }
197}